package ngamejava2;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.geom.Rectangle2D;

public class ActorObject extends GameObject<GamePlane>
{
	public ActorObject()
	{
		super();
		setCollidable(true);
		getBounds().width = 30;
		getBounds().height = 50;
	}

	@Override
	public void load()
	{
	}

	@Override
	public void unload()
	{
	}

	@Override
	public void update(long currentTick)
	{
		if (InputManager.getInstance().isPressed(KeyEvent.VK_UP))
		{
			moveBy(0f, -5f);
		}
		if (InputManager.getInstance().isPressed(KeyEvent.VK_DOWN))
		{
			moveBy(0f, 5f);
		}
		if (InputManager.getInstance().isPressed(KeyEvent.VK_LEFT))
		{
			moveBy(-5f, 0f);
		}
		if (InputManager.getInstance().isPressed(KeyEvent.VK_RIGHT))
		{
			moveBy(5f, 0f);
		}
		getParent().getParent().centerCameraAt(this);
	}

	@Override
	public void render(Graphics2D g, long currentFrame)
	{
		g.setColor(Color.yellow);
		g.fill(getBounds());
	}

	public boolean moveBy(float dx, float dy)
	{
		return moveByX(dx) && moveByY(dy);
	}

	public boolean moveByX(float dx)
	{
		return moveByX(dx, true);
	}

	public boolean moveByX(float dx, boolean slide)
	{
		if (dx != 0)
		{
			float x = getBounds().x;
			getBounds().x += dx;
			Rectangle2D.Float ir = new Rectangle2D.Float();
			if (getParent().getFirstInsersecting(this, ir) != null)
			{
				if (dx < 0)
				{
					getBounds().x += ir.width;
				} else
				{
					getBounds().x -= ir.width;
				}
				if (slide)
				{
					if (!moveByY(1, false))
					{
						moveByY(-1, false);
					}
				}
				return false;
			}
		}
		return true;
	}

	public boolean moveByY(float dy)
	{
		return moveByY(dy, true);
	}

	public boolean moveByY(float dy, boolean slide)
	{
		if (dy != 0)
		{
			float y = getBounds().y;
			getBounds().y += dy;
			Rectangle2D.Float ir = new Rectangle2D.Float();
			GameObject co = getParent().getFirstInsersecting(this, ir);
			if (co != null)
			{
				if (dy < 0)
				{
					getBounds().y += ir.height;
				} else
				{
					getBounds().y -= ir.height;
				}
				if (slide)
				{
					if (!moveByX(1, false))
					{
						moveByX(-1, false);
					}
				}
				return false;
			}
		}
		return true;
	}
}